package com.zxk.crawler.service.impl;

import com.alibaba.fastjson.JSON;
import com.zxk.core.common.model.FundAnalysise;
import com.zxk.crawler.service.EsDataHandleService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author zxk
 * @DATE 2022/7/27 13:51
 * @Version 1.0
 **/
@Service
public class EsDataHandleServiceImpl implements EsDataHandleService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public SearchResponse simpleSearchNews(String date,String... fields) {
        FieldSortBuilder sortBuilder = SortBuilders.fieldSort("newsTime").order(SortOrder.DESC);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchPhraseQuery("createTime",date));
        sourceBuilder.size(10000);
        sourceBuilder.sort(sortBuilder);
        if(null != fields) {
            sourceBuilder.fetchSource(fields,null);
        }
        SearchRequest searchRequest = new SearchRequest("news");
        searchRequest.source(sourceBuilder);
        try {
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            return search;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> handleSearchResult(SearchResponse searchResponse) {
        SearchHit[] hits = searchResponse.getHits().getHits();
        List<Map<String, Object>> collect = Arrays.stream(hits).map(hit -> hit.getSourceAsMap()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void saveFundAnalysise(FundAnalysise fundAnalysise) {
        try {
            IndexRequest request = new IndexRequest("fund_analysis_data");
            // 规则 PUT /index/_doc/1
            request.timeout(TimeValue.timeValueSeconds(10));
            // 将数据放入请求 json
            request.source(JSON.toJSONString(fundAnalysise), XContentType.JSON).type("_doc");
            restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
